package com.dd.cloud.user.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.config.common.ProfilesProperties;
import com.dd.cloud.common.constants.ProductConstants;
import com.dd.cloud.common.constants.ZekeConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.user.entity.product.PayParam;
import com.dd.cloud.user.enums.ExceptionEnum;
import com.dd.cloud.user.mapper.product.PayParamMapper;
import com.dd.cloud.user.service.product.IPayParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 支付参数表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-09-23
 */
@Service
public class PayParamServiceImpl extends ServiceImpl<PayParamMapper, PayParam> implements IPayParamService {
    @Autowired
    private ProfilesProperties profilesProperties;

    /**
     * 获取支付参数
     *
     * @param partyId
     * @return
     */
    @Override
    public PayParam getPayParam(Integer partyId, Integer partyType, String payType) {
        QueryWrapper<PayParam> qw = new QueryWrapper<>();
        qw.lambda().eq(PayParam::getPartyId, partyId)
                .eq(PayParam::getPartyType, partyType)
                .eq(PayParam::getPayType, payType)
                .eq(PayParam::getStatus, ProductConstants.PayParamStatus.NOMER);
        PayParam payParam = getOne(qw);
        if (payParam == null) {
            throw new ZekeException(ExceptionEnum.GET_PAY_PARAM_ERROR, "数据不存在");
        }
        return payParam;
    }

    /**
     * 获取支付参数, 通过appid
     *
     * @param appid
     * @return
     */
    @Override
    public PayParam getPayParamByAppid(String appid) {
        QueryWrapper<PayParam> qw = new QueryWrapper<>();
        qw.lambda().eq(PayParam::getAppId, appid)
                .eq(PayParam::getStatus, ProductConstants.PayParamStatus.NOMER);
        PayParam payParam = getOne(qw);
        if (payParam == null) {
            throw new ZekeException(ExceptionEnum.GET_PAY_PARAM_ERROR, "数据不存在");
        }
        return payParam;
    }

    /**
     * 初始化药房支付参数
     *
     * @param pharId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initPharmacyPayParam(Integer pharId) {
        PayParam payParam = new PayParam();
        payParam.setPartyId(pharId);
        payParam.setPartyType(2);
        payParam.setPayType("wx");
        payParam.setAppId("wx4b5b31862521d9d0");
        switch (profilesProperties.getActive()) {
            case ZekeConstants.ProfilesType.PROD:
                payParam.setNotifyUrl("http://admin.dadaotcm.com/dd/user/order/pay/notice/");
                payParam.setRefundNotify("http://admin.dadaotcm.com/dd/user/order/pay/refund/");
                payParam.setCertName("/usr/local/app/paypath/ak/ak_cert.p12");
                break;
            case ZekeConstants.ProfilesType.TEST:
                payParam.setNotifyUrl("http://test.zz-health.top/dd/user/order/pay/notice/");
                payParam.setRefundNotify("http://test.zz-health.top/dd/user/order/pay/refund/");
                payParam.setCertName("/usr/local/apps/dd/paypath/ak/ak_cert.p12");
                break;
            default:
        }
        payParam.setMchId("1641917413");
        payParam.setMchKey("tNxvKLRdvhs8cFJjpYjhJKzuRZLBpkXp");
        payParam.setVersion("V2");
        payParam.setBody("广州阿康中医");
        payParam.setSubject("广州阿康中医");
        payParam.setStatus(ProductConstants.PayParamStatus.NOMER);
        payParam.setCreateDate(LocalDateTime.now());
        save(payParam);
    }
}
